package com.chalk.config.security;

import com.chalk.config.security.filter.FilterIgnoreAuthConfig;
import com.chalk.config.security.filter.JwtBasicAuthFilter;
import com.chalk.config.security.filter.JwtUserLoginAuthFilter;
import com.chalk.service.impl.MemberDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

/**
 * @date: 2019-04-01 下午 07:05
 * @author: zhaobo0119@outlook.com
 * @Descriptopn:
 */
@EnableWebSecurity
/*注解式权限*/
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MemberDetailServiceImpl memberDetailService;

    @Autowired
    private FilterIgnoreAuthConfig ignoreAuthConfig;

    @Autowired
    private CustomAuthExceptionEntryPoint authExceptionEntryPoint;

    @Autowired
    private JwtRedisUtil jwtRedisUtil;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(memberDetailService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http
                .authorizeRequests();
        /* 忽略认证资源  */
        ignoreAuthConfig.getUrls().forEach(url -> {
            registry.antMatchers(url).permitAll();
        });

        /* 除上面外的所有请求全部需要鉴权认证 */
        registry.anyRequest().authenticated();
        http.cors().and().csrf().disable()
                .addFilter(new JwtUserLoginAuthFilter(authenticationManager(), jwtRedisUtil))
                .addFilter(new JwtBasicAuthFilter(authenticationManager(), jwtRedisUtil,authExceptionEntryPoint))
                // 不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.exceptionHandling().authenticationEntryPoint(authExceptionEntryPoint);
    }


    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }


    // 加密密码的，安全第一嘛~
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
